package sprs;
 

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import oracle.jdbc.pool.OracleDataSource;

import java.io.*;
import java.sql.DriverManager;

import org.eclipse.jetty.util.log.Log;

public class Connectors {

    static OracleDataSource oracleDS = null;
    static OracleDataSource fifiDS = null;
    final static Properties config = new Properties();

    static {

        try {
            config.load(Connectors.class.getClassLoader().getResourceAsStream(
                    "config.properties"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static {

        try {
            Class.forName("oracle.jdbc.OracleDriver");
            oracleDS = new OracleDataSource();
            oracleDS.setURL(config.getProperty("fifi_db_url"));
            oracleDS.setUser(config.getProperty("fifi_db_user"));
            oracleDS.setPassword(config.getProperty("fifi_db_pass"));
            Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                    try {

                        oracleDS.close();

                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getFifiCon() throws SQLException {
        Connection con = oracleDS.getConnection();
        if (Log.isDebugEnabled()) {
            con = QMUtil.getSpyConnection(con);
        }

        return con;
    }
    static OracleDataSource NRSoracleDS = null;
    static OracleDataSource NRSfifiDS = null;
    final static Properties NRSconfig = new Properties();

    static {

        try {
            NRSconfig.load(Connectors.class.getClassLoader()
                    .getResourceAsStream("config.properties"));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static {

        try {
            Class.forName("oracle.jdbc.OracleDriver");
            NRSoracleDS = new OracleDataSource();
            System.out.println(config.getProperty("db_url"));
            NRSoracleDS.setURL(config.getProperty("db_url"));
            NRSoracleDS.setUser(config.getProperty("db_user"));
            NRSoracleDS.setPassword(config.getProperty("db_pass"));
            Runtime.getRuntime().addShutdownHook(new Thread() {
                @Override
                public void run() {
                    try {

                        NRSoracleDS.close();

                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getNRSCon() throws SQLException {
        //if (true) return getQACon();
        Connection con = NRSoracleDS.getConnection();

        if (Log.isDebugEnabled()) {
            con = QMUtil.getSpyConnection(con);
        }
        return con;
    }

    public static Connection getQACon() throws SQLException {
        Connection con = null;
        Properties connectionProps;
        //net.sf.log4jdbc.ConnectionSpy.
        connectionProps = new Properties();
        connectionProps.put("user", "zw251y[NRS]");
        connectionProps.put("password", "qa246800");
        con = DriverManager.getConnection("jdbc:oracle:thin:@q1cnn1d1.hydc.sbc.com:1532:Q1NRS", connectionProps);
        if (Log.isDebugEnabled()) {
            con = QMUtil.getSpyConnection(con);
        }
        return con;
    }

    public static void main(String[] args) throws Exception {

        Connection con = getFifiCon();
        con.close();
    }
}